package flc.ast.adapter;

import android.graphics.Color;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import flc.ast.R;
import flc.ast.bean.WatchedBean;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;
import stark.common.bean.StkResBean;

public class SignAdapter extends StkProviderMultiAdapter<StkResBean> {

    public SignAdapter() {
        addItemProvider(new StkSingleSpanProvider(126));
        addItemProvider(new SignProvider());
    }

    private int type;

    public void setType(int type) {
        this.type = type;
    }

    private class SignProvider extends BaseItemProvider<StkResBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_sign;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, StkResBean bean) {

            Glide.with(baseViewHolder.itemView).load(bean.getThumbUrl())
                    .into((ImageView) baseViewHolder.getView(R.id.ivSignItemImg));
            baseViewHolder.setText(R.id.tvSignItemName, bean.getName());
            baseViewHolder.setText(R.id.tvSignItemDesc, bean.getDesc());

            String tag = "";
            for (String s : bean.getTagNameList()) {
                tag += s + " / ";
            }

            String str = new Gson().toJson(bean.getExtra());

            try {
                if (str != null && !str.equals("\"\"")) {
                    JSONObject extra = new JSONObject(str);
                    baseViewHolder.setText(R.id.tvSignItemType, extra.optString("releaseDate") + " " + tag);
                }
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }

            String text = "";

            if (type == 0) {
                text = getContext().getString(R.string.had_watched);
                List<WatchedBean> listSp = AppSpUtils.getWatchedList();
                if (listSp != null && listSp.size() > 0) {
                    for (WatchedBean watchedBean : listSp) {
                        if (watchedBean.getBean().getUrl().equals(bean.getUrl())) {
                            bean.setSelected(true);
                            break;
                        }
                    }
                }
            } else if (type == 1) {
                text = getContext().getString(R.string.want_to_watch);
                List<StkResBean> listSp = AppSpUtils.getWantWatchList();
                if (listSp != null && listSp.size() > 0) {
                    for (StkResBean stkResBean : listSp) {
                        if (stkResBean.getUrl().equals(bean.getUrl())) {
                            bean.setSelected(true);
                            break;
                        }
                    }
                }
            } else if (type == 2) {
                text = getContext().getString(R.string.watching);
                List<StkResBean> listSp = AppSpUtils.getWatchingList();
                if (listSp != null && listSp.size() > 0) {
                    for (StkResBean stkResBean : listSp) {
                        if (stkResBean.getUrl().equals(bean.getUrl())) {
                            bean.setSelected(true);
                            break;
                        }
                    }
                }
            }

            if (bean.isSelected()) {
                baseViewHolder.setBackgroundColor(R.id.tvSignItemSign, Color.parseColor("#D8D8D8"));
                baseViewHolder.setTextColor(R.id.tvSignItemSign, Color.parseColor("#69333333"));
                baseViewHolder.setText(R.id.tvSignItemSign, text);
            } else {
                baseViewHolder.setBackgroundColor(R.id.tvSignItemSign, Color.parseColor("#0A68FF"));
                baseViewHolder.setTextColor(R.id.tvSignItemSign, Color.parseColor("#FFFFFF"));
                baseViewHolder.setText(R.id.tvSignItemSign, getContext().getString(R.string.sign));
            }

        }
    }

}
